﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AssessmentTool.PresentationLogic.Controllers;
using AdminApplication.Controls;
using AssessmentTool.PresentationLogic;
using AdminApplication.PresentationLogic.EventArguments;
using System.IO;
using AssessmentTool.PresentationLogic.EventArguments;
using AssessmentTool;

namespace AdminApplication.PresentationLogic.Controllers
{
    public class AdminUserViewController : AbstractController<AdminView>
    {
        public AdminUserViewController(AdminView element)
            : base(element)
        {
            ed.subscrive("ADMINLOGIN", onLogin);
            ed.subscrive("LOGOUT", logout);
            ed.subscrive("BACKLISTAUTENTI", onLogin);
            ed.subscrive("CONSOLIDATO", onLogin);
            ed.subscrive("SAVECSV", saveCSV);
            ed.subscrive("SAVEEXCEL", saveExcel);
        }
        public void onLogin(EventArgs e)
        {
            MenuBuilder.getInstance().build("ADMIN-MAIN");
            ed.dispatch("DISABLELEGEND", null);
            element.Visibility = Visibility.Visible;
        }
        public void logout(EventArgs e)
        {
            element.Visibility = Visibility.Collapsed;
        }
        public void saveCSV(EventArgs e)
        {
            StringEventArgs evento = (StringEventArgs)e;
            SaveFileDialog d = new SaveFileDialog();
            d.Filter = "CSV file format|*.csv";
            if (d.ShowDialog() == true)
            {

                Stream s = d.OpenFile();
                StreamWriter sw = new StreamWriter(s);
                sw.Write(evento.stringbuilder.ToString());
                sw.Flush();
                s.Flush();
                sw.Close();
                s.Close();
                

            }
        }
        public void saveExcel(EventArgs e)
        {
            FileDownloadedEventArgs evento = (FileDownloadedEventArgs)e;
            SaveFileDialog d = new SaveFileDialog();
            d.Filter = "Excel file format|*.xls";
            if (d.ShowDialog() == true)
            {

                try
                {
                    Stream s = d.OpenFile();
                    MemoryStream ms = new MemoryStream(evento.file);
                    ms.WriteTo(s);
                    s.Flush();
                    s.Close();
                    ms.Close();
                }
                catch (Exception exc)
                {
                    ModalWindow ew = new ModalWindow();
                    ew.Show("Errore!", "Impossibile salvare il file...Assicurarsi che non sia protetto da scrittura o che non sia in uso. Vuoi riprovare?", ModalType.YESNO, "SAVEEXCEL", e);
                    ed.dispatch("STOPBUSY", new EventArgs());
                }

            }
        }
    }
}
